package com.human.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.human.entity.Emp;
import com.human.entity.ResultVo;
import com.human.service.IEmpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jzq
 * @since 2023-02-14
 */
@RestController
@RequestMapping("/api/emp")
@Api(tags = "员工管理")
@ApiSupport(order = 1)
public class EmpController {

    @Autowired
    IEmpService empService;


    /**
     * 登录
     *
     * @param emp
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "登录")
    public ResultVo<Emp> login(@RequestBody Emp emp, HttpServletRequest request) {
        return empService.login(emp, request);
    }

    /**
     * 退出
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation(value = "退出")
    public ResultVo<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return new ResultVo<>(true, "退出成功");
    }

    /**
     * 修改员工信息
     *
     * @param emp
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改员工信息")
    public ResultVo<String> update(@RequestBody Emp emp) {
        return empService.update(emp);
    }

    /**
     * 新增员工
     *
     * @param emp
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("新增员工")
    public ResultVo<String> save(@RequestBody Emp emp) {
        return empService.saveEmp(emp);
    }

    /**
     * 员工分页查询
     *
     * @param current
     * @param pageSize
     * @return
     */
    @PostMapping("/page")
    @ApiOperation(value = "员工分页查询")
//    @PreAuthorize("hasAuthority('api:emp:page')")
    public ResultVo<Page> page(int current, int pageSize, String name) {
        return empService.page(current, pageSize, name);
    }

    /**
     * 模糊查询
     *
     * @param name
     * @return
     */
    @GetMapping("/queryEmpByName")
    @ApiOperation(value = "模糊查询")
    public ResultVo<List> queryEmpByName(String name) {
        return empService.queryEmpByName(name);
    }


    /**
     * 根据ID查询员工
     *
     * @param workId
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询员工")
    public ResultVo getById(@PathVariable("id") Integer id) {
        return empService.getEmpById(id);
    }


    /**
     * 查询所有员工
     *
     * @return
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询所有员工")
//    @PreAuthorize("hasAuthority('api:emp:getAll')")
    public ResultVo getAllEmp() {
        return empService.getAllEmp();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据ID删除员工")
    public ResultVo delEmp(@PathVariable("id") Integer id) {
        return empService.delEmp(id);
    }


    @GetMapping("/getRestNum")
    @ApiOperation(value = "查询休假人数")
    public ResultVo getRestNum() {
        return empService.getRestNum();
    }

    @GetMapping("/getWorkNum")
    @ApiOperation(value = "查询在职人数")
    public ResultVo getWorkNum() {
        return empService.getWorkNum();
    }

    @GetMapping("/getLeaveNum")
    @ApiOperation(value = "查询离职人数")
    public ResultVo getLeaveNum() {
        return empService.getLeaveNum();
    }

}


